﻿using HCQ2_Common;
using HCQ2_Model;
using HCQ2_Model.ViewModel;
using HCQ2_Model.VR;
using HCQ2_Model.VR.Params;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace HCQ2UI_Logic.DocumentController
{
    /// <summary>
    ///  账户日志控制器
    /// </summary>
    public class AccountManagerController: BaseLogic
    {
        #region 1.0 用户管理首次进入页面 +ActionResult List()
        /// <summary>
        ///  用户管理首次进入页面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Element]
        [HCQ2_Common.Attributes.Load]
        [HttpGet]
        public ActionResult List()
        {
            return View();
        }
        #endregion

        #region 1.1 获取用户一栏数据 +ActionResult GetUserData()
        /// <summary>
        ///  获取用户一栏数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetGoldData(GoldParam param)
        {
            int total;
            List<T_VRGoldLogModel> goldList = operateContext.bllSession.T_VRGoldLog.GetGoldList(param, out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = goldList
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        //*********************************2.0销售管理************************************
        #region 1.0 销售管理首次进入页面 +ActionResult SellList()
        /// <summary>
        ///  销售管理首次进入页面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Element]
        [HCQ2_Common.Attributes.Load]
        [HttpGet]
        public ActionResult SellList()
        {
            return View();
        }
        #endregion

        #region 1.1 获取销售一栏数据 +ActionResult GetSellData(SellGoldParam param)
        /// <summary>
        ///  获取销售一栏数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetSellData(SellGoldParam param)
        {
            int total;
            List<T_VRSellGoldLogModel> sellList = operateContext.bllSession.T_VRGoldLog.GetSellGoldList(param, out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = sellList
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        //*********************************3.0用户等级管理************************************
        #region 3.0 用户等级管理首次进入页面 +ActionResult SellList()
        /// <summary>
        ///  0用户等级管理首次进入页面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Element]
        [HCQ2_Common.Attributes.Load]
        [HttpGet]
        public ActionResult LevelList()
        {
            return View();
        }
        #endregion

        #region 3.1 获取用户等级一栏数据 +ActionResult GetUserLevelData(UserLevelModel param)
        /// <summary>
        ///  获取用户等级一栏数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetUserLevelData(UserlevelParam param)
        {
            int total;
            List<UserLevelModel> sellList = operateContext.bllSession.T_VRUserLevel.GetUserLevelData(param, out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = sellList
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 3.2 添加用户等级 + ActionResult AddUserLevel(UserLevelModel model)
        /// <summary>
        ///  添加用户等级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddUserLevel(UserLevelModel model)
        {
            try
            {
                if(model.user_level<=0)
                    return operateContext.RedirectAjax(1, "请选择用户等级~", "", "");
                if(model.min_line<=0 && model.min_sale<=0)
                    return operateContext.RedirectAjax(1, "最低销售额、最低下限人数两者不能同时为空~", "", "");
                if(model.max_sale>0 && model.min_sale<=0)
                    return operateContext.RedirectAjax(1, "最大销售额有值时最小销售额不能为空~", "", "");
                if (model.max_line > 0 && model.min_line <= 0)
                    return operateContext.RedirectAjax(1, "最大下限人数有值时最小下限人数不能为空~", "", "");
                model.create_id= operateContext.Usr.user_id;
                model.create_name = operateContext.Usr.user_name;
                model.update_id = operateContext.Usr.user_id;
                model.update_name = operateContext.Usr.user_name;
                int mark = operateContext.bllSession.T_VRUserLevel.Add(model.ToEntry());
                if (mark > 0)
                    return operateContext.RedirectAjax(0, "用户等级添加成功~", "", "");
                return operateContext.RedirectAjax(1, "用户等级添加失败~", "", "");
            }
            catch (Exception ex)
            {
                return operateContext.RedirectAjax(1, ex.Message, "", "");
            }
        }
        #endregion

        #region 3.3 编辑用户等级 + ActionResult EditUserLevel(UserLevelModel model)
        /// <summary>
        ///  编辑用户等级
        /// </summary>
        /// <param name="model">用户等级对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditUserLevel(UserLevelModel model)
        {
            try
            {
                if(model.id<=0)
                    return operateContext.RedirectAjax(1, "更新主键值不允许为空~", "", "");
                if (model.user_level <= 0)
                    return operateContext.RedirectAjax(1, "请选择用户等级~", "", "");
                if (model.min_line <= 0 && model.min_sale <= 0)
                    return operateContext.RedirectAjax(1, "最低销售额、最低下限人数两者不能同时为空~", "", "");
                if (model.max_sale > 0 && model.min_sale <= 0)
                    return operateContext.RedirectAjax(1, "最大销售额有值时最小销售额不能为空~", "", "");
                if (model.max_line > 0 && model.min_line <= 0)
                    return operateContext.RedirectAjax(1, "最大下限人数有值时最小下限人数不能为空~", "", "");
                model.update_name = operateContext.Usr.user_name;
                model.update_id = operateContext.Usr.user_id;
                //编辑关卡课件
                int mark = operateContext.bllSession.T_VRUserLevel.Modify(model.ToEntry(), s => s.id == model.id, "user_level", "min_sale", "max_sale", "min_line", "max_line", "update_id", "update_name", "update_date", "level_note", "price_rate");
                return operateContext.RedirectAjax(0, "编辑用户等级成功~", "", "");
            }
            catch (Exception ex)
            {
                return operateContext.RedirectAjax(1, ex.Message, "", "");
            }
        }
        #endregion

        #region 3.4 删除用户等级 +ActionResult DelUserLevel(int id)
        /// <summary>
        ///  删除用户等级
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelUserLevel(int id)
        {
            if (id<=0)
                return operateContext.RedirectAjax(1, "关键字不允许为空~", "", "");
            int mark = operateContext.bllSession.T_VRUserLevel.SelectCount(s => s.id == id);
            if(mark<=0)
                return operateContext.RedirectAjax(1, "待删除的记录不存在~", "", "");
            mark = operateContext.bllSession.T_VRUserLevel.Delete(s => s.id == id);
            if (mark>0)
                return operateContext.RedirectAjax(0, "数据删除成功~", "", "");
            return operateContext.RedirectAjax(1, "数据删除失败~", "", "");
        }
        #endregion

        //*********************************3.0 用户等级日志************************************
        #region 3.0 用户等级日志首次进入页面 +ActionResult UserLevelLog()
        /// <summary>
        ///  用户等级日志首次进入页面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Element]
        [HCQ2_Common.Attributes.Load]
        [HttpGet]
        public ActionResult UserLevelLog()
        {
            return View();
        }
        #endregion

        #region 1.1 获取用户一栏数据 +ActionResult GetUserLevelLogData()
        /// <summary>
        ///  获取用户一栏数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetUserLevelLogData(UserLevelLogParam param)
        {
            int total;
            List<UserLevelLog> sellList = operateContext.bllSession.T_VRUserLevelLog.GetUserLevelLog(param, out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = sellList
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        //*********************************4.0 商品折扣************************************
        #region 4.0 商品折扣首次进入页面 +ActionResult GoodDisList()
        /// <summary>
        ///  商品折扣首次进入页面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Element]
        [HCQ2_Common.Attributes.Load]
        [HttpGet]
        public ActionResult GoodDisList()
        {
            return View();
        }
        #endregion

        #region 4.1 获取商品折扣列表 +ActionResult GetGoodDisData()
        /// <summary>
        ///  获取商品折扣列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetGoodDisData()
        {
            TableModel tModel = new TableModel()
            {
                total = 0,
                rows = null
            };
            string fileCode = RequestHelper.GetStrByName("fileCode");
            if(string.IsNullOrEmpty(fileCode))
                return Json(tModel, JsonRequestBehavior.AllowGet);
            List<T_VRGoodDiscount> sellList = operateContext.bllSession.T_VRGoodDiscount.Select(s => s.file_code.Equals(fileCode));
            tModel.total = sellList.Count;
            tModel.rows = sellList;
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 4.2 添加商品折扣 + ActionResult AddGoodDis(T_VRGoodDiscount model)
        /// <summary>
        ///  添加商品折扣
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddGoodDis(T_VRGoodDiscount model)
        {
            try
            {
               if(string.IsNullOrEmpty(model.file_code))
                    return operateContext.RedirectAjax(1, "商品信息不全~", "", "");
                var file = operateContext.bllSession.T_DocumentInfo.FindSingle(s => s.file_code.Equals(model.file_code) && s.file_status==5 && !s.if_remove);
                if (file == null)
                    return operateContext.RedirectAjax(1, "当前商品后台已下架或已删除~", "", "");
                model.file_id = file.file_id;
                model.file_name = file.file_name;
                model.create_id = operateContext.Usr.user_id;
                model.create_name = operateContext.Usr.user_name;
                model.create_date = DateTime.Now;
                model.update_id = operateContext.Usr.user_id;
                model.update_name = operateContext.Usr.user_name;
                model.update_date = DateTime.Now;
                int mark = operateContext.bllSession.T_VRGoodDiscount.Add(model);
                if (mark > 0)
                    return operateContext.RedirectAjax(0, "商品折扣添加成功~", "", "");
                return operateContext.RedirectAjax(1, "商品折扣添加失败~", "", "");
            }
            catch (Exception ex)
            {
                return operateContext.RedirectAjax(1, ex.Message, "", "");
            }
        }
        #endregion

        #region 4.3 编辑商品折扣 + ActionResult EditGoodDis(T_VRGoodDiscount model)
        /// <summary>
        ///  编辑商品折扣
        /// </summary>
        /// <param name="model">用户等级对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditGoodDis(T_VRGoodDiscount model)
        {
            try
            {
                if(model.id<=0)
                    return operateContext.RedirectAjax(1, "关键字id不允许缺失~", "", "");
                model.update_name = operateContext.Usr.user_name;
                model.update_id = operateContext.Usr.user_id;
                model.update_date = DateTime.Now;
                //编辑关卡课件
                int mark = operateContext.bllSession.T_VRGoodDiscount.Modify(model, s => s.id == model.id, "user_level", "rate", "note", "update_id", "update_name", "update_date");
                return operateContext.RedirectAjax(0, "编辑商品折扣成功~", "", "");
            }
            catch (Exception ex)
            {
                return operateContext.RedirectAjax(1, ex.Message, "", "");
            }
        }
        #endregion

        #region 4.4 删除商品折扣 +ActionResult DelGoodDisById(int id)
        /// <summary>
        ///  删除商品折扣
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelGoodDisById(int id)
        {
            if (id <= 0)
                return operateContext.RedirectAjax(1, "关键字不允许为空~", "", "");
            int mark = operateContext.bllSession.T_VRGoodDiscount.SelectCount(s => s.id == id);
            if (mark <= 0)
                return operateContext.RedirectAjax(1, "待删除的记录不存在~", "", "");
            mark = operateContext.bllSession.T_VRGoodDiscount.Delete(s => s.id == id);
            if (mark > 0)
                return operateContext.RedirectAjax(0, "数据删除成功~", "", "");
            return operateContext.RedirectAjax(1, "数据删除失败~", "", "");
        }
        #endregion

        //*********************************5.0 积分管理************************************
        #region 5.0 积分管理首次进入页面 +ActionResult IntegralList()
        /// <summary>
        ///  积分管理首次进入页面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Element]
        [HCQ2_Common.Attributes.Load]
        [HttpGet]
        public ActionResult IntegralList()
        {
            return View();
        }
        #endregion

        #region 5.1 获取积分一栏数据 +ActionResult GetTotalData(TotalLogParam param)
        /// <summary>
        ///  获取用户一栏数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetTotalData(TotalLogParam param)
        {
            int total;
            List<UserIntegralLogModel> totalList = operateContext.bllSession.T_VRUserLevel.GetTotalGoldList(param, out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = totalList
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion        

        //*********************************6.0 创始人协议管理************************************
        #region 6.0 获取创始人协议 +ActionResult GetFounderData(int id)
        /// <summary>
        ///  获取创始人协议
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetFounderData(int id)
        {
            if(id<=0)
                return operateContext.RedirectAjax(1, "关键字为空~", "", "");
            var obj = operateContext.bllSession.T_VRFounderDeal.FindSingle(o => o.folder_id == id);
            if(obj==null)
                return operateContext.RedirectAjax(0, "成功获取数据", "", "");
            return operateContext.RedirectAjax(0, "成功获取数据", obj, "");
        }
        #endregion

        #region 6.1 添加编辑创始人协议 + ActionResult AddEditFounderData(T_VRFounderDeal model)
        /// <summary>
        ///  添加编辑创始人协议
        /// </summary>
        /// <param name="model">用户等级对象</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddEditFounderData(T_VRFounderDeal model)
        {
            try
            {
                int bus_id=RequestHelper.GetIntByName("bus_id"),
                    founder_id = RequestHelper.GetIntByName("founder_id");
                if (bus_id <= 0)
                    return operateContext.RedirectAjax(1, "商家信息为空~", "", "");
                if (founder_id <= 0)
                {
                    model.folder_id = bus_id;
                    model.create_id = operateContext.Usr.user_id;
                    model.create_name = operateContext.Usr.user_name;
                    model.create_date = DateTime.Now;
                    model.update_id = operateContext.Usr.user_id;
                    model.update_name = operateContext.Usr.user_name;
                    model.update_date = DateTime.Now;
                    //添加
                    operateContext.bllSession.T_VRFounderDeal.Add(model);
                }
                else
                {
                    model.folder_id = bus_id;
                    model.update_id = operateContext.Usr.user_id;
                    model.update_name = operateContext.Usr.user_name;
                    model.update_date = DateTime.Now;
                    //编辑
                    operateContext.bllSession.T_VRFounderDeal.Modify(model, s => s.id == founder_id, "deal_note", "update_id", "update_name", "update_date");
                }
                return operateContext.RedirectAjax(0, "", "", "");
            }
            catch (Exception ex)
            {
                return operateContext.RedirectAjax(1, ex.Message, "", "");
            }
        }
        #endregion
        #region  积分来源首次进入页面 +ActionResult PointSource()
        /// <summary>
        ///  积分来源首次进入页面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Element]
        [HCQ2_Common.Attributes.Load]
        [HttpGet]
        public ActionResult PointSource()
        {
            return View();
        }
        #endregion
        #region  积分来源首次进入页面 +ActionResult PointSourceDetails()
        /// <summary>
        ///  积分详细首次进入页面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Element]
        [HCQ2_Common.Attributes.Load]
        [HttpGet]
        public ActionResult PointSourceDetails(int pointId)
        {
            pointId = RequestHelper.GetIntByName("pointId");
            return View();
        }
        #endregion

    }
}